<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/2/18
 * Time: 下午9:06
 */
namespace Admin\Controller;
use Common\Controller\AdminBaseController;
use Lib\MyTree;
use Lib\Tree;

class ClassificationController extends AdminBaseController
{
    public function index()
    {
        $classification_data = M('classification')->field('id,classification,pid,add_time')->order('pid asc')->select();
        $class_tree = new MyTree();
        $classification_info = $class_tree->my_tree($classification_data);
        $this->assign('classification_info',$classification_info);
        $this->assign('title','TD商城商品分类展示');
        $this->display('index');
    }

    public function add()
    {
        if(IS_AJAX){
            //获取商品分类名
            $classification_name = I('post.classification_name','');
            if('' == $classification_name){
                $this->ajaxReturn(['state'=>1,'msg'=>'商品分类名不能为空！']);
            }
            //获取商品父级分类ID
            $pid = I('post.pid',0);
            if(D('classification')->where(array('classification'=>$classification_name,'pid'=>$pid))->getField('id')){
                $this->ajaxReturn(['state'=>1,'msg'=>'此商品分类已经存在！']);
            }
            if(D('classification')->add(['classification'=>$classification_name,'pid'=>$pid,'add_time'=>time()])){
                $this->ajaxReturn(['state'=>0,'msg'=>'添加此商品分类成功！']);
            }
            $this->ajaxReturn(['state'=>1,'msg'=>'添加此商品分类失败！']);
        }else{
            $classification_data = D('classification')->get_many('','id,classification,pid','pid asc');
            $class_tree = new MyTree();
            $classification_info = $class_tree->my_tree($classification_data);
            $this->assign('classification_info',$classification_info);
            $this->assign('title','FD商城添加商品分类');
            $this->display();
        }
    }

    public function edit()
    {
        if(IS_GET){
            $id = I('get.id',0);
            if($id == 0){
                $this->error('非法操作！');
            }
            $one_classification = D('classification')->get_one_info(['id'=>$id],'id,classification,pid');
            $classification_data = D('classification')->get_many('','id,classification,pid','pid asc');
            $class_tree = new MyTree();
            $classification_info = $class_tree->my_tree($classification_data);
            $this->assign('one_classification',$one_classification);
            $this->assign('classification_info',$classification_info);
            $this->assign('title','FD商城添加商品分类');
            $this->display();
        }
        if(IS_POST){
            //获取要修改的商品分类ID
            $id = I('post.id',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            //获取商品分类名
            $classification_name = I('post.classification_name','');
            if(empty($classification_name)){
                $this->ajaxReturn(['state'=>1,'msg'=>'商品分类名不能为空！']);
            }
            //获取商品分类父级ID
            $pid = I('post.pid',0);
            $classification = D('classification');
            if(!$classification->get_field('id',['id'=>$id])){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能修改不存在的商品分类！']);
            }
            if($classification->get_field('id',['pid'=>$pid,'classification'=>$classification_name])){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能修改为已经存在的商品分类！']);
            }
            if(!$classification->where(['id'=>$id])->setField(['pid'=>$pid,'classification'=>$classification_name])){
                $this->ajaxReturn(['state'=>1,'msg'=>'修改失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'修改成功！']);
        }
    }

    public function del()
    {
        if(IS_POST){
            $id = I('post.id/d',0);
            if(empty($id)){
                $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
            }
            $classification = D('classification');
            if($classification->get_field('id',['id'=>$id]) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除不存在的商品分类！']);
            }
            if($classification->get_field('id',['pid'=>$id])){
                $this->ajaxReturn(['state'=>1,'msg'=>'不能删除有孩子的商品分类！']);
            }
            if($classification->delete($id) == false){
                $this->ajaxReturn(['state'=>1,'msg'=>'删除失败！']);
            }
            $this->ajaxReturn(['state'=>0,'msg'=>'删除成功！']);
        }else{
            $this->ajaxReturn(['state'=>1,'msg'=>'非法操作！']);
        }
    }
}